"""
Case Type   : cm_ctl
Case Name   : 资源池化集群停止集群后执行cm_ctl pause、启动集群
Create At   : 2024/04/05
Owner       : @kyeleze
Description :
    1.停止集群
    2.执行cm_ctl pause
    3.查看集群状态
    4.执行cm_ctl resume
    5.查看集群状态
    6.执行cm_ctl start
    7.查看集群状态
Expect      :
    1.成功
    2.成功
    3.查不到集群状态，Maybe cm_server is not running
    4.成功 
    5.查不到集群状态，Maybe cm_server is not running
    6.成功
    7.集群状态正常，不处于暂停状态
History     :
"""

import os
import unittest
import time

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from yat.test import Node

primary_sh = CommonSH('PrimaryDbUser')


@unittest.skipIf(3 != primary_sh.get_node_num(), '非1+2环境不执行')
class CmPauseResumeCase(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.constant = Constant()
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.pri_user = Node("PrimaryDbUser")
        self.pri_root = Node("PrimaryRoot")
        self.common = Common()
        self.pause_file = os.path.join('$GAUSSHOME', 'bin/cluster_manual_pause')

    def test_cm(self):
        step = '--step1:停止集群 expect:成功--'
        self.log.info(step)
        result = self.primary_sh.exec_cm_ctl('stop')
        self.log.info(result)
        self.assertIn(self.constant.cm_stop_success_msg, result,
                      "执行失败:" + step)

        step = '--step2:执行cm_ctl pause expect:成功--'
        self.log.info(step)
        result = self.primary_sh.exec_cm_ctl('pause')
        self.log.info(result)
        self.assertIn(self.constant.cm_pause_success_msg, result,
                      "执行失败:" + step)

        step = '--step3:查看集群状态 expect:查不到集群状态,' \
                'Maybe cm_server is not running--'
        self.log.info(step)
        result = self.primary_sh.exec_cm_ctl('query', '-Cvidpw')
        self.log.info(result)
        self.assertIn("Maybe cm_server is not running", result,
                      "执行失败:" + step)

        step = '--step4:执行cm_ctl resume expect:成功--'
        self.log.info(step)
        result = self.primary_sh.exec_cm_ctl('resume')
        self.log.info(result)
        self.assertIn(self.constant.cm_resume_success_msg, result,
                      "执行失败:" + step)

        step = '--step5:查看集群状态 expect:查不到集群状态,' \
               'Maybe cm_server is not running--'
        self.log.info(step)
        result = self.primary_sh.exec_cm_ctl('query', '-Cvidpw')
        self.log.info(result)
        self.assertIn("Maybe cm_server is not running", result,
                      "执行失败:" + step)

        step = '--step6:执行cm_ctl start expect:成功--'
        self.log.info(step)
        result = self.primary_sh.exec_cm_ctl('start')
        self.log.info(result)
        self.assertIn(self.constant.cm_start_success_msg, result,
                      "执行失败:" + step)

        step = '--step7:查看集群状态 expect:集群状态正常，不处于暂停状态--'
        self.log.info(step)
        result = self.primary_sh.exec_cm_ctl('query', '-Cvidpw')
        self.log.info(result)
        self.assertIn(self.constant.CLUSTER_NORMAL_MSG, result,
                      "执行失败:" + step)
        self.assertNotIn('pausing         : Yes', result,
                         "执行失败:" + step)

    def tearDown(self):
        step = '----恢复环境----'
        self.log.info(step)
        result = self.primary_sh.exec_cm_ctl("resume")
        self.log.info(result)
        self.assertIn(self.constant.cm_resume_success_msg, result,
                      "执行失败:" + step)
        self.log.info(f'----{os.path.basename(__file__)} end----')
